//
//  FileManager.m
//  mmtouch
//
//  Created by xing lion on 11-9-6.
//  Copyright 2011年 tencent. All rights reserved.
//

#import "FileManager.h"
static FileManager *singletonfile = nil; 

@interface FileManager (Private)
-(BOOL) fileExist:(NSString*)name;
-(NSData*)readDataFromFile:(NSString*)name;
-(void)writeDataToFile:(NSData*)data fileName:(NSString*)name;
-(NSString*) getFileFullName:(NSString*)name;
-(NSDictionary *)dictionaryWithContentsOfData:(NSData *)data;
@end

@implementation FileManager

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

+(id) sharedInstance
{
    @synchronized(self)
    {
        if (singletonfile == nil)
        {
            
            singletonfile = [[FileManager alloc] init];
        }
        
    }
    
    return singletonfile;
}

-(NSData*)openFile:(NSString*)name
{
    if ([self fileExist:name] ) {
        
        return [self readDataFromFile:name];
    }

    return nil;
}


-(void)saveData:(NSData*)data fileName:(NSString*)name;
{

    [self writeDataToFile:data fileName:name];
}


-(BOOL) fileExist:(NSString*)name
{

    
    NSString* fullname =  [self getFileFullName:name];
    
    return  [[NSFileManager defaultManager] fileExistsAtPath:fullname];

}


-(NSData*)readDataFromFile:(NSString*)name
{
   
    NSString* fullname = [self getFileFullName:name];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:fullname]) {
        
        NSData* data = [NSData dataWithContentsOfFile:fullname];
        
        return  data;
    }

    return nil;
}


-(void)writeDataToFile:(NSData*)data fileName:(NSString*)name
{

    NSString* fullname =  [self getFileFullName:name];
    
    [data writeToFile:fullname atomically:YES];

}

-(NSString*) getFileFullName:(NSString*)name
{

    NSArray *patharray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString* pathname = [patharray objectAtIndex:0];
    
    pathname =   [pathname stringByAppendingString:@"/"];
    
    return [pathname stringByAppendingFormat:name];
    
    //return @"/Users/xinglion/mm.dat";

}


-(NSDictionary*)getMMPicStatus
{

    if (_mmpicstatus == nil) {
        
        
        NSData* data=  [self openFile:@"mm.dat"];
        
        if (data == nil) {
            
            _mmpicstatus = [[NSMutableDictionary alloc] initWithCapacity:10];
            for (int i = 1; i <= 88; i++) {
                NSString *name = [NSString stringWithFormat:@"%d.jpg",i];
                [_mmpicstatus setValue:@"NO" forKey:name];
                [_mmpicstatus setValue:[NSNumber numberWithInt:0] forKey:[name stringByAppendingFormat:@"step"]];
                
            }
            
            return _mmpicstatus;
        }
        
      _mmpicstatus =  (NSMutableDictionary*)[self dictionaryWithContentsOfData:data];
        
        if (_mmpicstatus ) {
            
            [_mmpicstatus retain];
        }
        else
        {
            
            _mmpicstatus = [[NSMutableDictionary alloc] initWithCapacity:10];
            for (int i = 1; i <= 88; i++) {
                NSString *name = [NSString stringWithFormat:@"%d.jpg",i];
                [_mmpicstatus setValue:@"NO" forKey:name];
                [_mmpicstatus setValue:[NSNumber numberWithInt:0] forKey:[name stringByAppendingFormat:@"step"]];
               
            }
        
        }
        
    }
    
    return _mmpicstatus;
}

-(NSDictionary *)dictionaryWithContentsOfData:(NSData *)data { 
    CFPropertyListRef plist =  CFPropertyListCreateFromXMLData(kCFAllocatorDefault, (CFDataRef)data, 
                                                               kCFPropertyListImmutable, 
                                                               NULL); 
    if(plist == nil) return nil; 
    if ([(id)plist isKindOfClass:[NSDictionary class]]) { 
        return [(NSDictionary *)plist autorelease]; 
    } 
    else { 
        CFRelease(plist); 
        return nil; 
    } 
}

+(void)releaseFileManager
{

    if (singletonfile) {
        
        [singletonfile saveDictionaryTofile];
        
        [singletonfile release];
    }
}

-(void)saveDictionaryTofile
{
    if (_mmpicstatus == nil) {
        
        return;
    }
    
     NSData* data = [NSPropertyListSerialization dataFromPropertyList:_mmpicstatus format:NSPropertyListXMLFormat_v1_0
                                     errorDescription:nil];
    if (data) {
        
        [self saveData:data fileName:@"mm.dat"];
        
    }
    

}

-(void) dealloc
{   

    if (_mmpicstatus) {
        
        [_mmpicstatus release];
        _mmpicstatus = nil;
    }
}


@end
